<?php

namespace Forum\ForumBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\TextField;
use Symfony\Component\Form\TextareaField;

use Forum\ForumBundle\Entity\Topic;
use Forum\ForumBundle\Entity\Message;
use Forum\ForumBundle\Entity\CategorieTopic;


class TopicForm extends Form {

    /**
     * $validation:NotBlank
     */
    public $topic;

    /**
     * $validation:NotBlank
     */
    public $message;

    public function configure(){

        $this->add(new TextField('topic'));
        $this->add(new TextareaField('message'));

    }

    public function save($em){

        $topic = new Topic();
        $topic->setNom_topic($this->topic);
        $topic->setDate_topic(\date_create(date('Y-m-d H:i:s')));

        // auteur du topic
        $membre = $em->getRepository('Blog\AccBundle\Entity\Membre')->findOneBy(array('id_membre' => 1));

        $categorie = $em->getRepository('Forum\ForumBundle\Entity\CategorieTopic')->findOneBy(array('id_categorie' => 1));

        $topic->setCategorieTopic($categorie);
        $topic->setMembre($membre);

        $em->persist($topic);
        //$em->flush();

        $message = new Message();
        $message->setContenu_message($this->message);
        $message->setTopic($topic);
        $message->setMembre($membre);
      
        $message->setDate_message(\date_create(date('Y-m-d H:i:s')));

        //echo $topic->getId_topic();


        $em->persist($message);
        $em->flush();

    }


}
?>
